package com.example.javamaday.codetop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CodeTop0001 {

    public static void main(String[] args) {

        new CodeTop0001().twoSum(new int[]{2,7,11,15}, 9);
    }

    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer, List<Integer>> numberMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            List<Integer> list = numberMap.get(nums[i]);
            if (list == null) {
                list = new ArrayList<>();
                list.add(i);
                numberMap.put(nums[i], list);
            } else {
                list.add(i);
                numberMap.put(nums[i], list);
            }
        }
        for (int i = 0; i < nums.length; i++) {
            List<Integer> list = numberMap.get(target - nums[i]);
            if (list == null) {
                continue;
            } else {
                for (int j = 0; j < list.size(); j++) {
                    if (list.get(j) == i) {
                        continue;
                    } else {
                        return new int[]{i, list.get(j)};
                    }
                }
            }
        }
        return new int[]{};
    }
}
